package com.xiayuanxing.arithmetic.DataStructures.Queue;

import java.util.Scanner;

/**
 * @program: arithmetic
 * @description: 数组模拟队列
 * @author: xia yuan xing
 * @create: 2021-08-31 10:15
 */
public class CircleArrayQueue {

    public static void main(String[] args) {
        //测试数组模拟环形
        CircleArray circleArray = new CircleArray(4);

        //接收用户输入
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出队列");
            System.out.println("a(add): 添加队列");
            System.out.println("g(get): 获取队列中的数据");
            System.out.println("h(head): 查看队列头部数据");
            //接收用户输入的字符串
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    circleArray.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数字：");
                    int num = scanner.nextInt();
                    circleArray.addQueue(num);
                    break;
                case 'g':
                    try {
                        int n = circleArray.getQueue();
                        System.out.printf("取出的数据是%d\n", n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int n = circleArray.headQueue();
                        System.out.printf("取出的头部数据是%d\n", n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出！");
    }
}

//使用数组模拟一个环形队列 -编写一个CircleArray
class CircleArray {
    //表示数组的最大容量
    private int maxSize;
    private int front;//指向队列的第一个元素，也就是说arr[front]
    private int rear;//指向队列的最后一个元素的后一个位置
    private int[] arr;//该数组用于存放数据，模拟队列


    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    //判断队列是否满？
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    //判断队列是否为空
    public boolean isNull() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int num) {
        //判队列是否满？
        if (isFull()) {
            System.out.println("队列已经满了");
            return;
        }
        arr[rear] = num;
        //让rear后移
        rear = (rear + 1) % maxSize;

    }

    //获取队列数据
    public int getQueue() {
        if (isNull()) {
            throw new RuntimeException("队列为空，无数据可取出");
        }
        //1.先把front对应的值保留到一个临时变量
        //2.将front后移 考虑取模
        //3.将保存的临时变量返回
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    //显示队列的所有数据
    public void showQueue() {
        if (isNull()) {
            System.out.println("队列为空");
        }
        //思路：从front开始遍历
        for (int i = 0; i < front + effectiveNum(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //显示队列头数据
    public int headQueue() {
        //判断队列是否为空
        if (isNull()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }

    //获取当前队列有效数据个数
    public int effectiveNum() {
        return (rear + maxSize - front) % maxSize;
    }
}